<template>
    <div>
        <el-button type="primary" plain @click="add">添加</el-button>
        <el-table
            :data="bannerlist"
            row-key="id"
            border
            style="width: 100%"
        >
            <el-table-column prop="id" label="编号" width="100">
            </el-table-column>
            <el-table-column prop="title" label="轮播图标题">
            </el-table-column>
            <el-table-column prop="img" label="图片">
                <template slot-scope="item" v-if="item.row.img">
                    <el-image
                        style="width: 100%"
                        :src="$host+item.row.img"
                        fit="fill"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <el-tag type="success" plain size="middle">{{
                        scope.row.status == 1 ? "启用" : "禁用"
                    }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="operate" label="操作" ref="banner">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        icon="el-icon-edit"
                        circle
                        @click="edit(scope.row)"
                    ></el-button>
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        circle
                        @click="del(scope.row.id)"
                    ></el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
//引入vuex
import Vuex from "vuex";
export default {
    methods: {
        //引入 分类列表 接口
        ...Vuex.mapActions({
            getlist: "banner/bannerlist_a",
            bannerdel: "banner/bannerdelete_a",
            bannerinfo: "banner/bannerinfo_a",
        }),
        //点击添加
        add() {
            this.$emit("changeBtn",true)
        },
        //点击编辑
        edit(row) {
            this.$emit("changeBtn", false); //点击编辑 不是添加按钮
            this.bannerinfo(row.id);
            //如果有img图片，那么上传给fileList文件列表
            if(row.img){
                //把fileList的数据变成img,使得点击编辑的时候可以查看到图片
                this.$store.commit('banner/setfileList',[{name:row.bannername,url:this.$host + row.img}])
            }
        },
        //点击删除
        del(id) {
            //点击弹出
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.bannerdel(id);
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
    },
    mounted() {
        //调用
        this.getlist();
    },
    computed: {
        //引入 分类列表数据
        ...Vuex.mapGetters({
            bannerform: "banner/getbannerinfo",
            bannerlist: "banner/getbannerlist",
        }),
    },
};
</script>

<style scoped></style>
